package com.roamer.audition.job.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 定时任务配置
 *
 * @author roamer
 * @version v1.0
 * @date 2019/10/6 17:43
 */
@EnableScheduling
@Configuration
public class ScheduledConfig implements SchedulingConfigurer {

    /**
     * 任务线程池
     *
     * @param taskRegistrar 任务注册器
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    /**
     * 定时任务线程池
     *
     * @return {@code ScheduledExecutorService}
     */
    @Bean(destroyMethod = "shutdown")
    public ScheduledExecutorService taskExecutor() {
        return new ScheduledThreadPoolExecutor(4, new ThreadFactoryBuilder().setNameFormat("task-pool-%d").build());
    }
}
